package dfl.bysj.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import dfl.bysj.config.RedisKey;
import dfl.bysj.config.model.VO.UserDto;
import dfl.bysj.entity.Honest;
import dfl.bysj.entity.HonestGrade;
import dfl.bysj.entity.UserTeacherStudent;
import dfl.bysj.mapper.HonestGradeeMapper;
import dfl.bysj.service.HonestGradeService;
import dfl.bysj.service.UserTeacherStudentService;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DFL
 * @since 2022-04-26
 */
@Service
@NoArgsConstructor
public class HonestGradeServiceImpl extends ServiceImpl<HonestGradeeMapper, HonestGrade> implements HonestGradeService {

    private RedisTemplate redisTemplate;


    private List<HonestGrade> honestGrades;


    private AtomicReference<HonestGrade> honestGradeAtomicReference= new AtomicReference<>(new HonestGrade());

    private UserTeacherStudentService userTeacherStudentService;

    @Autowired
    public void setUserTeacherStudentService(UserTeacherStudentService userTeacherStudentService) {
        this.userTeacherStudentService = userTeacherStudentService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private void getHonestGrades(){
        honestGrades= (List<HonestGrade>) redisTemplate.opsForValue().get(RedisKey.ALL_HONESTGRADE);
        if (honestGrades==null){
            updateRedisOfHonestGrades();
        }
    }

    private void updateRedisOfHonestGrades(){
        honestGrades=list();
        redisTemplate.opsForValue().set(RedisKey.ALL_HONESTGRADE,honestGrades,7, TimeUnit.DAYS);
    }

    @Override
    public HonestGrade getHonestGradeByStuUseId(Integer stuUseId) {
        getHonestGrades();
        honestGrades.stream()
                .filter(s->s.getStuUseId().equals(stuUseId))
                .forEach(s->honestGradeAtomicReference.set(s));
        if ( honestGradeAtomicReference.get().getId()==null){
            return null;
        }
        return  honestGradeAtomicReference.get();
    }

    @Override
    public HonestGrade updateHonestGradeByHonestStudent(Integer stuUseId, Float grade) {
        getHonestGrades();
        HonestGrade honestGrade=new HonestGrade();
        //问题：如果是新用户要新增用户分
        float grade_;
        //先拿到原来的分数
        honestGrades.stream()
                .filter(s->s.getStuUseId().equals(stuUseId))
                .forEach(s->honestGradeAtomicReference.set(s));
        if (honestGradeAtomicReference.get().getId()==null){
            //新增信用分
            HonestGrade honestGrade1=new HonestGrade(stuUseId,100-grade,0);
            save(honestGrade1);
        }else{
            grade_=honestGradeAtomicReference.get().getGrade();
            grade_-=grade;
            honestGrade.setGrade(grade_);
            update(honestGrade,new QueryWrapper<HonestGrade>()
                    .eq("stu_use_id", stuUseId));
        }
        updateRedisOfHonestGrades();
        return getHonestGradeByStuUseId(stuUseId);
    }

    @Override
    public HonestGrade getHonestGradeByStuId(int parseInt) {
       UserTeacherStudent userTeacherStudent= userTeacherStudentService.getOne(
                new QueryWrapper<UserTeacherStudent>()
                        .eq("stu_id", parseInt)
                        .eq("check_state", true));

       getHonestGrades();
       honestGrades
               .stream()
               .filter(s->s.getStuUseId().equals(userTeacherStudent.getStuUseId()))
               .forEach(s->honestGradeAtomicReference.set(s));
        return honestGradeAtomicReference.get();
    }

    @Override
    public void updateHonestRecordByStuUseId(Integer stuUseId) {
        getHonestGrades();
        honestGrades.stream()
                .filter(s->s.getStuUseId().equals(stuUseId))
                .forEach(s->honestGradeAtomicReference.set(s));
        HonestGrade honestGrade=honestGradeAtomicReference.get();
        int record=honestGrade.getRecord();
        honestGrade.setRecord(record+1);
        update(honestGrade,
                new QueryWrapper<HonestGrade>()
                        .eq("stu_use_id", stuUseId));

        updateRedisOfHonestGrades();
    }

    @Override
    public void updateHonestGradeByStuUseId(HonestGrade honestGrade, Integer beforeStuUseId) {
        update(honestGrade, new QueryWrapper<HonestGrade>()
                .eq("stu_use_id", beforeStuUseId));
        updateRedisOfHonestGrades();
    }


}
